package xxl.signals.base.natives 
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.utils.getTimer;
	import xxl.signals.base.valuelists.ArrayValueListAscend;
	import xxl.signals.base.valuelists.EventValueListAscend;
	import xxl.signals.core.ILazySignal;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class LazyNativeRelaySignal extends NativeRelaySignal implements ILazySignal 
	{
		protected var valueObjectsList:EventValueListAscend = EventValueListAscend.NIL;
		
		public function LazyNativeRelaySignal(target:IEventDispatcher = null, eventType:String = "", eventClass:Class = null) 
		{
			super(target, eventType, eventClass);
		}
		
		override protected function onNativeEvent(e:Event):void 
		{
			valueObjectsList = valueObjectsList.append(e);
		}
		
		/* INTERFACE xxl.signals.core.ILazySignal */
		
		public function activate(limitTime:int = 0):Boolean 
		{
			var process:EventValueListAscend = valueObjectsList;
			if (process.nonEmpty)
			{
				const time:int = getTimer();
				if (sortFlag)
				{
					sortFlag = false;
					slots = slots.sortOn("priority", Array.NUMERIC);
				}
				while (process.nonEmpty)
				{
					const event:Event = process.tail;
					process = process.head;
					for (var i:int = slots.length; i--; )
					{
						slots[i].execute1(event);
					}
					if ((limitTime > 0) && (limitTime < (getTimer() - time)))
					{
						break;
					}
				}
				if (process.nonEmpty)
				{
					process.first = valueObjectsList.first;
					valueObjectsList = process;
					return false;
				}
				else
				{
					valueObjectsList = EventValueListAscend.NIL;
					return true;
				}
			}
			return true;
		}
		
	}

}